import 'package:flutter/material.dart';

class ClipRoute extends StatelessWidget {
  const ClipRoute({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var avatar = Image.asset("assets/icons/logo.png", width: 60.0);
    return Scaffold(
      appBar: AppBar(
        title: const Text('剪裁（Clip）'),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: <Widget>[
            const SizedBox(height: 10),
            avatar,
            const SizedBox(height: 10),
            // 剪裁为圆形
            ClipOval(child: avatar),
            const SizedBox(height: 10),
            //剪裁为圆角矩形
            ClipRRect(child: avatar, borderRadius: BorderRadius.circular(5)),
            const SizedBox(height: 10),

            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Align(
                  alignment: Alignment.topLeft,
                  widthFactor: 0.5,
                  child: avatar,
                ),
                const Text(
                  "你好世界",
                  style: TextStyle(color: Colors.green),
                ),
              ],
            ),
            const SizedBox(height: 10),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                ClipRect(
                  child: Align(
                    alignment: Alignment.topLeft,
                    widthFactor: 0.5,
                    child: avatar,
                  ),
                ),
                const Text(
                  "你好世界",
                  style: TextStyle(color: Colors.green),
                ),
              ],
            ),
            const SizedBox(height: 10),
            DecoratedBox(
              decoration: const BoxDecoration(color: Colors.red),
              child: ClipRect(
                child: avatar,
                clipper: MyClipper(),
              ),
            )
          ],
        ),
      ),
    );
  }
}

class MyClipper extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) => const Rect.fromLTRB(10.0, 15.0, 40.0, 30.0);

  @override
  bool shouldReclip(covariant CustomClipper<Rect> oldClipper) => false;
}
